
public class Node {

    public Comparable data;
    public Node left, right;
    public int height = 1;

    public boolean isEmpty() {
        return this.data == null;
    }

    public boolean isLeaf() {
        return left.isEmpty() && right.isEmpty();
    }

    public void height() {
        if (isLeaf()) {
            height = 1;
        } else if (left.isEmpty()) {
            height = 1 + right.height;
        } else if (right.isEmpty()) {
            height = 1 + left.height;
        } else {
            height = 1 + Math.max(left.height, right.height);
        }
    }

    public int bal() {
        if (isLeaf()) {
            return 0;
        } else if (left.isEmpty()) {
            return right.height;
        } else if (right.isEmpty()) {
            return -left.height;
        } else {
            return right.height - left.height;
        }
    }
}
